#include "deptmanager.h"
#include "editdept.h"

DeptManager::DeptManager(Ui::Index *ui,QObject *parent)
    : QObject{parent}
{
    this->ui = ui;
    //图标
    QPixmap pix(":/image/res/search.jpg");
    pix.scaled(25,25);
    ui->btn_deptManager_hideSearch->setIcon(pix);
    ui->btn_deptManager_search->setIcon(pix);
    QPixmap freshPix(":/image/res/refresh.jpg");
    ui->btn_deptManager_reset->setIcon(freshPix.scaled(25,25));
    ui->btn_deptManager_refresh->setIcon(freshPix.scaled(25,25));
    isExpand = 0;

    init();

    connect(ui->btn_deptManager_add,&QPushButton::clicked,this,[=](){
        EditDept *e = new EditDept();
        e->setAttribute(Qt::WA_DeleteOnClose); // 设置标志，在关闭对话框时自动删除
//        connect(e,&EditDept::success,this,[=](){
//            getList();
//            initTree();
//        });
        e->exec();
    });
    connect(ui->btn_deptManager_search,&QPushButton::clicked,this,[=](){
        ui->btn_deptManager_search->setEnabled(0);
        getList();
        initTree();
        ui->btn_deptManager_search->setEnabled(1);
    });
    connect(ui->btn_deptManager_reset,&QPushButton::clicked,this,[=](){
        ui->btn_deptManager_reset->setEnabled(0);
        ui->lineEdit_deptManager_deptName->clear();
        ui->comboBox_deptManager_status->setCurrentIndex(-1);
        getList();
        initTree();
        ui->btn_deptManager_reset->setEnabled(1);
    });
    connect(ui->btn_deptManager_hideSearch,&QPushButton::clicked,this,[=](){
        ui->widget_52->setHidden(!ui->widget_52->isHidden());
    });
    connect(ui->btn_deptManager_refresh,&QPushButton::clicked,this,[=](){
        ui->btn_deptManager_refresh->setEnabled(0);
        getList();
        initTree();
        ui->btn_deptManager_refresh->setEnabled(1);
    });
    connect(ui->btn_deptManager_expand,&QPushButton::clicked,this,[=](){
        isExpand ? ui->treeWidget_deptManager->collapseAll() : ui->treeWidget_deptManager->expandAll();
        isExpand = !isExpand;
    });
}


void DeptManager::init()
{
    getType();
    getList();
    initTree();
}

void DeptManager::getType()
{
    ui->comboBox_deptManager_status->clear();
    QByteArray *data = utils.getDictType("sys_normal_disable");
    QJsonObject obj = QJsonDocument::fromJson(*data).object();
    QJsonArray arr = obj.value("data").toArray();
    for(int i = 0;i< arr.size();i++){
        sys_normal_disable.append(DictData::toDictData(arr.at(i).toObject()));
    }
    for(int i = 0;i< sys_normal_disable.size();i++){
        ui->comboBox_deptManager_status->addItem(sys_normal_disable.at(i).dictLabel);
        ui->comboBox_deptManager_status->setItemData(i,sys_normal_disable.at(i).dictValue);
    }
    ui->comboBox_deptManager_status->setCurrentIndex(-1);
}

void DeptManager::getList()
{
    deptList.clear();
    QString deptName = ui->lineEdit_deptManager_deptName->text();
    QString status = ui->comboBox_deptManager_status->currentIndex() == -1 ? "" :
        ui->comboBox_deptManager_status->currentData().value<QString>();
    QByteArray *data = api.list(deptName,status);
    QJsonObject obj = QJsonDocument::fromJson(*data).object();
    QJsonArray arr = obj.value("data").toArray();
    for(int i = 0;i< arr.size();i++){
        deptList.append(Dept::toDept(arr.at(i).toObject()));
    }
}

void DeptManager::initTree()
{
    QTreeWidget *tree = ui->treeWidget_deptManager;
    tree->clear();
    tree->setColumnCount(6);
    tree->header()->setDefaultAlignment(Qt::AlignCenter);
    tree->header()->setSectionsClickable(true);
    tree->hideColumn(5);
    tree->setColumnWidth(0,150);
    tree->setColumnWidth(1,48);
    tree->setColumnWidth(2,220);
    tree->setColumnWidth(3,200);
    tree->setColumnWidth(4,60);
    tree->setColumnWidth(5,180);
    for(int i = 0;i< deptList.size();i++){
        if(deptList.at(i).parentId == 0){
            QTreeWidgetItem *item = new QTreeWidgetItem(tree);
            for(int j = 0;j < 6;j++){
                item->setTextAlignment(j,Qt::AlignCenter);
                if(i % 3 == 0){
                    item->setBackground(j,QColor(249, 255, 238));
                }else if(i % 3 == 1){
                    item->setBackground(j,QColor(221, 255, 219));
                }else{
                    item->setBackground(j,QColor(226, 255, 252));
                }
            }
            item->setText(0,deptList.at(i).deptName);
            item->setText(1,QString::number(deptList.at(i).orderNum));
            item->setText(2,deptList.at(i).status == "0" ? "正常" : "停用");
            item->setText(3,deptList.at(i).createTime);
            QPushButton *update = new QPushButton("🖍️");
            QPushButton *add = new QPushButton("➕");
            update->setToolTip("修改");
            add->setToolTip("新增");
//            connect(update,&QPushButton::clicked,this,[=](){
//                Editdept *e = new Editdept(sys_show_hide,deptList[i].deptName,deptList[i].deptId,0);
//                connect(e,&Editdept::success,this,[=](){
//                    getList();
//                    initTree();
//                });
//                e->exec();
//            });
//            connect(add,&QPushButton::clicked,this,[=](){
//                Editdept *e = new Editdept(sys_show_hide,deptList[i].deptName,deptList[i].deptId,1);
//                connect(e,&Editdept::success,this,[=](){
//                    getList();
//                    initTree();
//                });
//                e->exec();
//            });
            update->setStyleSheet("QPushButton {padding: 10px 20px;border-style: none;border-radius: 15px;background-color: rgb(231, 250, 240);color: #ffffff;}QPushButton:hover {background-color: rgb(19, 206, 102);}QPushButton:pressed {background-color: rgb(19, 150, 102);}");
            add->setStyleSheet   ("QPushButton {padding: 10px 20px;border-style: none;border-radius: 15px;background-color: rgb(232, 244, 255);color: #ffffff;}QPushButton:hover {background-color: rgb(24, 144, 255);}QPushButton:pressed {background-color: rgb(24, 144, 200);}");
            QHBoxLayout *layout = new QHBoxLayout;
            layout->addWidget(update);
            layout->addWidget(add);
            QWidget *widget = new QWidget();
            widget->setLayout(layout);
            tree->setItemWidget(item, 4, widget);
            item->setText(5,QString::number(deptList.at(i).deptId));
            setChildTree(item);
        }
    }
}

void DeptManager::setChildTree(QTreeWidgetItem *item)
{
    for(int i = 0;i< deptList.size();i++){
        if(deptList.at(i).parentId == item->data(5,0).value<int>()){
            QTreeWidgetItem *child = new QTreeWidgetItem(item);
            for(int j = 0;j < 6;j++){
                child->setTextAlignment(j,Qt::AlignCenter);
                if(i % 3 == 0){
                    child->setBackground(j,QColor(249, 255, 238));
                }else if(i % 3 == 1){
                    child->setBackground(j,QColor(221, 255, 219));
                }else{
                    child->setBackground(j,QColor(226, 255, 252));
                }
            }
            child->setText(0,deptList.at(i).deptName);
            child->setText(1,QString::number(deptList.at(i).orderNum));
            child->setText(2,deptList.at(i).status == "0" ? "正常" : "停用");
            child->setText(3,deptList.at(i).createTime);
            QPushButton *update = new QPushButton("🖍️");
            QPushButton *add = new QPushButton("➕");
            QPushButton *del = new QPushButton("❌");
            update->setToolTip("修改");
            add->setToolTip("新增");
            del->setToolTip("删除");
//            connect(update,&QPushButton::clicked,this,[=](){
//                Editdept *e = new Editdept(sys_show_hide,deptList[i].deptName,deptList[i].deptId,0);
//                connect(e,&Editdept::success,this,[=](){
//                    getList();
//                    initTree();
//                });
//                e->exec();
//            });
//            connect(add,&QPushButton::clicked,this,[=](){
//                Editdept *e = new Editdept(sys_show_hide,deptList[i].deptName,deptList[i].deptId,1);
//                connect(e,&Editdept::success,this,[=](){
//                    getList();
//                    initTree();
//                });
//                e->exec();
//            });
//            connect(del,&QPushButton::clicked,this,[=](){
//                int flag  = QMessageBox::question(0,"提示","确定删除该项吗?",QMessageBox::Yes|QMessageBox::No);
//                if(flag == QMessageBox::Yes){
//                    if(flag == QMessageBox::No) return;
//                    QByteArray *bytes = api.deleteById(deptList.at(i).deptId);
//                    QJsonObject obj = QJsonDocument::fromJson(*bytes).object();
//                    if(200 == obj.value("code").toInt()){
//                        QMessageBox::warning(0,"提示",obj.value("msg").toString());
//                        getList();
//                        initTree();
//                    }
//                }
//            });
            update->setStyleSheet("QPushButton {padding: 10px 20px;border-style: none;border-radius: 15px;background-color: rgb(231, 250, 240);color: #ffffff;}QPushButton:hover {background-color: #2980b9;}QPushButton:pressed {background-color: #1177b9;}");
            add->setStyleSheet   ("QPushButton {padding: 10px 20px;border-style: none;border-radius: 15px;background-color: rgb(232, 244, 255);color: #ffffff;}QPushButton:hover {background-color: #2980b9;}QPushButton:pressed {background-color: #1177b9;}");
            del->setStyleSheet   ("QPushButton {padding: 10px 20px;border-style: none;border-radius: 15px;background-color: rgb(255, 237, 237);color: #ffffff;}QPushButton:hover {background-color: #2980b9;}QPushButton:pressed {background-color: #1177b9;}");
            QHBoxLayout *layout = new QHBoxLayout;
            layout->addWidget(update);
            layout->addWidget(add);
            layout->addWidget(del);
            QWidget *widget = new QWidget();
            widget->setLayout(layout);
            ui->treeWidget_deptManager->setItemWidget(child, 4, widget);
            child->setText(5,QString::number(deptList.at(i).deptId));
            item->addChild(child);
            setChildTree(child);
        }
    }
}